﻿#include "UCtrPanel.h"
#include <QMessageBox>
#include "CHeader.h"
UCtrPanel::UCtrPanel(QWidget *parent)
	: UBasePanel(parent)
{
	ui.setupUi(this);
	initUI();
	initConnect();
}

UCtrPanel::~UCtrPanel()
{
}

void UCtrPanel::setStyleSheetFile()
{
}
void UCtrPanel::initUI()
{
	ui.pushButtonControl->SetPushButtenText("", A2T("上升"), A2T("下降"), A2T("后退"), A2T("前进"));
	ui.PushButtenYt->SetPushButtenText("", A2T("上"), A2T("下"), A2T("左"), A2T("右"));
	ui.pushButtonFlush->hide();
}

void UCtrPanel::initData()
{
	if (m_pDevData->m_nIsHaveFlush == 0)
	{
		ui.pushButtonFlush->hide();
	}
	ui.pushButtonRobResetLoc->hide();
	m_nSpeed = 0;
}

void UCtrPanel::initConnect()
{
	//ui中的槽函数绑定
	connect(ui.pushButtonDeSpeed, &QPushButton::clicked, this, &UCtrPanel::slotPushButtonDeSpeedClicked);
	connect(ui.pushButtonAddSpeed, &QPushButton::clicked, this, &UCtrPanel::slotPushButtonAddSpeedClicked);
	connect(ui.pushButtonRobResetLoc, &QPushButton::clicked, this, &UCtrPanel::slotPushButtonResetLocClicked);
	connect(ui.pushButtonFlush, &QPushButton::clicked, this, &UCtrPanel::slotPushButtonFlushClicked);
	connect(ui.pushButtonTalkBackFunc, &QPushButton::clicked, this, &UCtrPanel::slotPushButtonTalkBackFuncClicked);
	connect(ui.pushButtonTalkBackClose, &QPushButton::clicked, this, &UCtrPanel::slotPushButtonTalkBackCloseClicked);
	connect(ui.pushButtonLampLightOpen, &QPushButton::clicked, this, &UCtrPanel::slotPushButtonLampLightOpenClicked);
	connect(ui.pushButtonLampLightClose, &QPushButton::clicked, this, &UCtrPanel::slotPushButtonLampLightCloseClicked);
	connect(ui.pushButtonFocusingAdd, &QPushButton::pressed, this, &UCtrPanel::slotPushButtonFocusingAddClicked);
	connect(ui.pushButtonFocusingAdd, &QPushButton::released, this, &UCtrPanel::slotPushButtonFocusingAddRelease);
	connect(ui.pushButtonFocusingSubtract, &QPushButton::pressed, this, &UCtrPanel::slotPushButtonFocusingSubtractClicked);
	connect(ui.pushButtonFocusingSubtract, &QPushButton::released, this, &UCtrPanel::slotPushButtonFocusingSubtractRelease);
	connect(ui.pushButtonBroadcastAudio, &QPushButton::clicked, this, &UCtrPanel::slotPushButtonBroadcastAudioClicked);

	//CPushButtonA中的槽函数绑定
	//机器人控制部分
	connect(ui.pushButtonControl, SIGNAL(PressedUp()), this, SLOT(slotRobRunUp()));       //上
	connect(ui.pushButtonControl, SIGNAL(PressedDown()), this, SLOT(slotRobRunDown()));   //下
	connect(ui.pushButtonControl, SIGNAL(PressedLeft()), this, SLOT(slotRobRunLeft()));  //前进
	connect(ui.pushButtonControl, SIGNAL(PressedRigth()), this, SLOT(slotRobRunRight()));  //后退
	connect(ui.pushButtonControl, SIGNAL(ReleasedUp()), this, SLOT(slotRobRunStop()));    //停止运动
	connect(ui.pushButtonControl, SIGNAL(ReleasedDown()), this, SLOT(slotRobRunStop()));  //停止运动
	connect(ui.pushButtonControl, SIGNAL(ReleasedLeft()), this, SLOT(slotRobRunStop()));  //停止运动
	connect(ui.pushButtonControl, SIGNAL(ReleasedRigth()), this, SLOT(slotRobRunStop())); //停止运动

	//云台控制部分
	connect(ui.PushButtenYt, SIGNAL(PressedUp()), this, SLOT(slotYtUpP()));               //云台 按下 上
	connect(ui.PushButtenYt, SIGNAL(PressedDown()), this, SLOT(slotYtDownP()));           //云台 按下 下
	connect(ui.PushButtenYt, SIGNAL(PressedLeft()), this, SLOT(slotYtLeftP()));           //云台 按下 左
	connect(ui.PushButtenYt, SIGNAL(PressedRigth()), this, SLOT(slotYtRigthP()));         //云台 按下 右
	connect(ui.PushButtenYt, SIGNAL(ReleasedUp()), this, SLOT(slotYtUpR()));              //云台 松开 上
	connect(ui.PushButtenYt, SIGNAL(ReleasedDown()), this, SLOT(slotYtDownR()));          //云台 松开 下
	connect(ui.PushButtenYt, SIGNAL(ReleasedLeft()), this, SLOT(slotYtLeftR()));          //云台 松开 左
	connect(ui.PushButtenYt, SIGNAL(ReleasedRigth()), this, SLOT(slotYtRigthR()));        //云台 松开 右
}

void UCtrPanel::slotPushButtonDeSpeedClicked()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(ui.pushButtonDeSpeed->text());
	sLog.m_bExecResult = true;
	sLog.m_sDevName = A2T("远程操控");
	m_pDevData->addDevExecLog(sLog);
	m_nSpeed--;
	if (m_nSpeed > 0)
	{
		if (m_nSpeed > 5)//速度限制
		{
			m_nSpeed = 5;
		}
	}
	else
	{
		m_nSpeed = 0;
	}

	double dSpeed = (double)m_nSpeed / 10;

	m_nSpeedShowCount = 0;
	ui.pushButtonControl->update();
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDevData->m_sDevGuid;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCSetSpeed;
	stCtrInfo.m_nSpeed = m_nSpeed;
	bool bRet = m_pRobCtrMange->ctrDevice(stCtrInfo);
	if (bRet)
	{
		QString sStr = QString::number(dSpeed) + "m/s";
		ui.pushButtonControl->SetPushButtenText(sStr);
	}
}

void UCtrPanel::slotPushButtonAddSpeedClicked()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(ui.pushButtonAddSpeed->text());
	sLog.m_bExecResult = true;
	sLog.m_sDevName = A2T("远程操控");
	m_pDevData->addDevExecLog(sLog);
	m_nSpeed++;
	if (m_nSpeed > 0)
	{
		if (m_nSpeed > 5)//速度限制
		{
			m_nSpeed = 5;
		}
	}
	else
	{
		m_nSpeed = 0;
	}

	double dSpeed = (double)m_nSpeed / 10;
	QString sStr = QString::number(dSpeed) + "m/s";
	ui.pushButtonControl->SetPushButtenText(sStr);

	m_nSpeedShowCount = 0;
	ui.pushButtonControl->update();
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDevData->m_sDevGuid;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCSetSpeed;
	stCtrInfo.m_nSpeed = m_nSpeed;
	bool bRet = m_pRobCtrMange->ctrDevice(stCtrInfo);
	if (bRet)
	{
		QString sStr = QString::number(dSpeed) + "m/s";
		ui.pushButtonControl->SetPushButtenText(sStr);
	}
}

void UCtrPanel::slotPushButtonResetLocClicked()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(ui.pushButtonRobResetLoc->text());
	sLog.m_bExecResult = true;
	sLog.m_sDevName = A2T("远程操控");
	m_pDevData->addDevExecLog(sLog);
	//触发是否复位
	int nRet = messageBox(3, A2T("是否确定进行复位?"));
	if (QMessageBox::No == nRet || QMessageBox::Close == nRet)
	{
		return;
	}
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDevData->m_sDevGuid;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCinitRob;
	m_pRobCtrMange->ctrDevice(stCtrInfo);
}

void UCtrPanel::slotPushButtonFlushClicked()//冲洗按钮点击槽函数
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(ui.pushButtonFlush->text());
	sLog.m_bExecResult = true;
	sLog.m_sDevName = A2T("远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo infoEn;
	infoEn.m_sGuid = m_pDevData->m_sDevGuid;
	infoEn.m_bCtrSync = true;
	infoEn.m_bHasCtr = false;
	infoEn.m_nCtrDevType = enOntology;
	infoEn.m_nCtrCmd = enCGoToFlushEn;
	bool bRet = m_pRobCtrMange->ctrDevice(infoEn);

	QEventLoop loop;
	QTimer::singleShot(500, &loop, SLOT(quit()));
	loop.exec();

	SCtrInfo infoOff;
	infoOff.m_sGuid = m_pDevData->m_sDevGuid;
	infoOff.m_bCtrSync = true;
	infoOff.m_bHasCtr = false;
	infoOff.m_nCtrDevType = enOntology;
	infoOff.m_nCtrCmd = enCGoToFlushOff;
	bRet = m_pRobCtrMange->ctrDevice(infoOff);
}

void UCtrPanel::slotPushButtonTalkBackFuncClicked()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(ui.pushButtonTalkBackFunc->text());
	sLog.m_bExecResult = true;
	sLog.m_sDevName = A2T("远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo info;
	info.m_nCtrCmd = enCVoiceTalk;
	info.m_bCtrSync = true;
	bool bRet = m_pCamerCtrMange->ctrDevice(info);
	if (bRet)
	{
		ui.pushButtonTalkBackFunc->setIsTagVisible(true);
		ui.pushButtonTalkBackClose->setIsTagVisible(false);
	}
}

void UCtrPanel::slotPushButtonTalkBackCloseClicked()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(ui.pushButtonTalkBackClose->text());
	sLog.m_bExecResult = true;
	sLog.m_sDevName = A2T("远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo info;
	info.m_bCtrSync = true;
	info.m_nCtrCmd = enCStopVoiceTalk;
	bool bRet = m_pCamerCtrMange->ctrDevice(info);
	if (bRet)
	{
		ui.pushButtonTalkBackFunc->setIsTagVisible(false);
		ui.pushButtonTalkBackClose->setIsTagVisible(true);
	}
}

void UCtrPanel::slotPushButtonLampLightOpenClicked()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(ui.pushButtonLampLightOpen->text());
	sLog.m_bExecResult = true;
	sLog.m_sDevName = A2T("远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo info;
	info.m_bCtrSync = true;
	info.m_nCtrCmd = enCLightPwoer;
	bool bRet = m_pCamerCtrMange->ctrDevice(info);
	if (bRet)
	{
		ui.pushButtonLampLightOpen->setIsTagVisible(true);
		ui.pushButtonLampLightClose->setIsTagVisible(false);
	}
}

void UCtrPanel::slotPushButtonLampLightCloseClicked()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(ui.pushButtonLampLightClose->text());
	sLog.m_bExecResult = true;
	sLog.m_sDevName = A2T("远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo info;
	info.m_nCtrCmd = enCStopLightPwoer;
	info.m_bCtrSync = true;
	bool bRet = m_pCamerCtrMange->ctrDevice(info);
	if (bRet)
	{
		ui.pushButtonLampLightOpen->setIsTagVisible(false);
		ui.pushButtonLampLightClose->setIsTagVisible(true);
	}
}

//调焦+按钮槽函数
void UCtrPanel::slotPushButtonFocusingAddClicked()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(ui.pushButtonFocusingAdd->text());
	sLog.m_bExecResult = true;
	sLog.m_sDevName = A2T("远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo info;
	info.m_nCtrCmd = enZoomAdd;
	info.m_bCtrSync = true;
	m_pCamerCtrMange->ctrDevice(info);
}

//调焦+按钮释放槽函数
void UCtrPanel::slotPushButtonFocusingAddRelease()
{
	SCtrInfo info;
	info.m_nCtrCmd = enZoomAddStop;
	info.m_bCtrSync = true;
	m_pCamerCtrMange->ctrDevice(info);
}

//调焦-按钮槽函数
void UCtrPanel::slotPushButtonFocusingSubtractClicked()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(ui.pushButtonFocusingSubtract->text());
	sLog.m_bExecResult = true;
	sLog.m_sDevName = A2T("远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo info;
	info.m_nCtrCmd = enZoomSubtract;
	info.m_bCtrSync = true;
	m_pCamerCtrMange->ctrDevice(info);
}

//调焦-按钮释放槽函数
void UCtrPanel::slotPushButtonFocusingSubtractRelease()
{
	SCtrInfo info;
	info.m_nCtrCmd = enZoomSubtractStop;
	info.m_bCtrSync = true;
	m_pCamerCtrMange->ctrDevice(info);
}

void UCtrPanel::slotPushButtonBroadcastAudioClicked()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(ui.pushButtonBroadcastAudio->text());
	sLog.m_bExecResult = true;
	sLog.m_sDevName = A2T("远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo info;
	info.m_nCtrCmd = enBroadcastAudio;
	info.m_bCtrSync = false;
	info.m_bIsLog = true;
	m_pCamerCtrMange->ctrDevice(info);
}

void UCtrPanel::slotRobRunUp()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(A2T("机械臂上升"));
	sLog.m_sDevName = A2T("机器人远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDevData->m_sDevGuid;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCMoveUp;
	m_pRobCtrMange->ctrDevice(stCtrInfo);
}

void UCtrPanel::slotRobRunDown()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(A2T("机械臂下降"));
	sLog.m_sDevName = A2T("机器人远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDevData->m_sDevGuid;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCMoveDown;
	m_pRobCtrMange->ctrDevice(stCtrInfo);
}

void UCtrPanel::slotRobRunRight()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(A2T("前进"));
	sLog.m_sDevName = A2T("机器人远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDevData->m_sDevGuid;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCMoveForward;
	m_pRobCtrMange->ctrDevice(stCtrInfo);
}

void UCtrPanel::slotRobRunLeft()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(A2T("后退"));
	sLog.m_sDevName = A2T("机器人远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDevData->m_sDevGuid;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCMoveBackward;
	m_pRobCtrMange->ctrDevice(stCtrInfo);
}

void UCtrPanel::slotRobRunStop()
{
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDevData->m_sDevGuid;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCNoCmd;
	m_pRobCtrMange->ctrDevice(stCtrInfo);
}

void UCtrPanel::slotYtUpP()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(A2T("云台上"));
	sLog.m_sDevName = A2T("机器人远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo info;
	info.m_nCtrCmd = enCTiltUp;
	m_pCamerCtrMange->ctrDevice(info);
}

void UCtrPanel::slotYtDownP()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(A2T("云台下"));
	sLog.m_sDevName = A2T("机器人远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo info;
	info.m_nCtrCmd = enCTiltDown;
	m_pCamerCtrMange->ctrDevice(info);
}

void UCtrPanel::slotYtRigthP()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(A2T("云台右"));
	sLog.m_sDevName = A2T("机器人远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo info;
	info.m_nCtrCmd = enCPanRight;
	m_pCamerCtrMange->ctrDevice(info);
}

void UCtrPanel::slotYtLeftP()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(A2T("云台左"));
	sLog.m_sDevName = A2T("机器人远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo info;
	info.m_nCtrCmd = enCPanLeft;
	m_pCamerCtrMange->ctrDevice(info);
}

void UCtrPanel::slotYtUpR()
{
	SCtrInfo info;
	info.m_nCtrCmd = enCTiltUpStop;
	m_pCamerCtrMange->ctrDevice(info);
}

void UCtrPanel::slotYtDownR()
{
	SCtrInfo info;
	info.m_nCtrCmd = enCTiltDownStop;
	m_pCamerCtrMange->ctrDevice(info);
}

void UCtrPanel::slotYtRigthR()
{
	SCtrInfo info;
	info.m_nCtrCmd = enCPanRightStop;
	m_pCamerCtrMange->ctrDevice(info);
}

void UCtrPanel::slotYtLeftR()
{
	SCtrInfo info;
	info.m_nCtrCmd = enCPanLeftStop;
	m_pCamerCtrMange->ctrDevice(info);
}